package com.songlin.testv2ex.broadcast_receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

import com.songlin.testv2ex.data.GlobalData;

/**
 * Created by 松林 on 2015/10/5.
 */
public class ConnectionStateReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context,Intent intent) {
/*        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo activeInfo = manager.getActiveNetworkInfo();*/
        try {
            ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null) {

                NetworkInfo info = connectivity.getActiveNetworkInfo();
                if (info != null && info.isConnected()) {

                    if (info.getState() == NetworkInfo.State.CONNECTED) {
                        GlobalData.Is_Connected = true;
                    }
                } else {
                    Log.d("ConnectionState", "false");
                    Toast.makeText(context, "亲，网络连了么？", Toast.LENGTH_LONG).show();
                    GlobalData.Is_Connected = false;
                }
            }
        } catch (Exception ex) {
            GlobalData.Is_Connected = false;
            Toast.makeText(context, "亲，网络连了么？", Toast.LENGTH_LONG).show();
            Log.d("Throws Exception", "Catch Exception From ConnectionStateReceiver.java");
            ex.printStackTrace();
        }
    }
}
